<?php
class Uploader
{

    #上传文件的数据
    protected $files;
    #上传文件的目录
    protected $dir;
    #对外开放接口
    #上传接口
    public function make(){
        $this->makeDir();
        $this->format();
        $uploadFiles = [];
        foreach ($this->files as $key => $val){
            if($val['error'] == 0){
                if(is_uploaded_file($val['tmp_name'])){
                    $uploadPath = $this->dir.DIRECTORY_SEPARATOR.time().mt_rand(0,99999).'.'.pathinfo($val['name'])['extension'];
                    move_uploaded_file($val['tmp_name'],$uploadPath);
                    $uploadFiles[] = [
                        'file' => $uploadPath,
                        'name' => $val['name']
                    ];
                }
            }
        }
        return $uploadFiles;
    }


    #创建目录
    #创建规则  时间
    public function makeDir(){
        $uploadDir = 'uploads/'.date('Y-m-d',time());
        $this->dir = $uploadDir;
        return is_dir($uploadDir) or mkdir($uploadDir,0777,true);
    }

    #私有接口
    #格式化
    public function format(){
        $files = [];
        foreach ($_FILES as $key => $val){
            if(is_array($val['name'])){
                foreach ($val['name'] as $index => $file){
                    $files[] = [
                        'name' => $file,
                        'type' => $val['type'][$index],
                        'error' => $val['error'][$index],
                        'size' => $val['size'][$index],
                        'tmp_name' => $val['tmp_name'][$index]
                    ];
                }
            }else{
                $files[] = $val;
            }
        }
        return $this->files = $files;
    }


}

